#include "../OptimizedWireDetector.hpp"
#include <iostream>
#include <iomanip>

using namespace wire_detection;

int main() {
    try {
        // 创建检测器（使用工厂函数）
        auto detector = create_balanced_detector();

        // 单张图像检测
        std::filesystem::path image_path = "test_images/pole_001.jpg";
        auto result = detector->detect_wire(image_path);

        if (result && ! result->error_message) {
            std::cout << "=== 检测结果 ===" << std::endl;
            std::cout << "是否有铁丝: " << (result->has_wire ? "是" : "否") << std::endl;
            std::cout << "置信度: " << std::fixed << std::setprecision(3)
                      << result->confidence_score << std::endl;
            std::cout << "处理时间: " << result->processing_time.count()
                      << " 秒" << std::endl;
            std::cout << "线条数量: " << result->line_count << std::endl;
            std::cout << "纹理复杂度: " << result->texture_ratio << std::endl;

            // 可视化结果
            auto vis_result = detector->visualize_detection(
                image_path, "output/detection_result.jpg");
            if (vis_result) {
                std::cout << "可视化结果已保存" << std::endl;
            }
        } else {
            std::cerr << "检测失败: " << result->error_message.value_or("未知错误")
                      << std::endl;
        }

        // 批量检测示例
        std::vector<std::filesystem::path id="W85lpEGfRan5yzg9krPlWB7xgwe"> image_paths = {
            "test_images/smooth_pole_001.jpg",
            "test_images/wire_wrapped_002.jpg",
            "test_images/complex_scene_003.jpg"
        };

        std::cout << "\n=== 批量检测结果 ===" << std::endl;
        auto batch_results = detector->batch_detect(image_paths);

        for (const auto& [path, result] : batch_results) {
            std::cout << "文件: " << std::filesystem::path(path).filename()
                      << " -> " << (result.has_wire ? "有铁丝" : "无铁丝")
                      << " (置信度: " << result.confidence_score << ")" << std::endl;
        }

        // 性能基准测试
        std::cout << "\n=== 性能基准测试 ===" << std::endl;
        const int iterations = 100;
        auto start = std::chrono::high_resolution_clock::now();

        for (int i = 0; i < iterations; ++i) {
            detector->detect_wire(image_path);
        }

        auto end = std::chrono::high_resolution_clock::now();
        auto total_time = std::chrono::duration<double id="Gh3mp7UqTa4nbxgRzAalK8vwgoc">(end - start);

        std::cout << "平均处理时间: " << (total_time.count() / iterations * 1000)
                  << " 毫秒" << std::endl;
        std::cout << "处理能力: " << (iterations / total_time.count())
                  << " 图像/秒" << std::endl;

    } catch (const std::exception& e) {
        std::cerr << "程序异常: " << e.what() << std::endl;
        return -1;
    }

    return 0;
}

// 高级使用示例：自定义配置和并行处理
void advanced_usage_example() {
    // 创建自定义配置
    DetectionConfig custom_config;
    custom_config.resize_height = 1200;  // 更高分辨率
    custom_config.texture_threshold = 0.12;
    custom_config.confidence_weights.texture = 0.5;  // 更重视纹理特征

    auto detector = std::make_unique<optimizedwiredetector id="ZMEcpwPQuawooKgp1EFleonygXc">(std::move(custom_config));

    // 使用结构化绑定和现代 C++ 特性
    std::filesystem::path test_dir = "test_images";
    std::vector<std::filesystem::path id="IAsTpdX4YafTVKgvqsLls4EIgNd"> all_images;

    // C++17 filesystem 遍历
    for (const auto& entry : std::filesystem::directory_iterator(test_dir)) {
        if (entry.is_regular_file()) {
            auto ext = entry.path().extension().string();
            std::transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
            if (ext == ".jpg" || ext == ".jpeg" || ext == ".png") {
                all_images.push_back(entry.path());
            }
        }
    }

    // 并行批量处理
    auto results = detector->batch_detect(all_images);

    // 使用现代 C++ 算法统计结果
    auto wire_count = std::count_if(results.begin(), results.end(),
        [](const auto& pair) { return pair.second.has_wire; });

    std::cout << "检测到铁丝的图像数量: " << wire_count
              << "/" << results.size() << std::endl;

    // 计算平均置信度
    double avg_confidence = std::accumulate(results.begin(), results.end(), 0.0,
        [](double sum, const auto& pair) {
            return sum + pair.second.confidence_score;
        }) / results.size();

    std::cout << "平均置信度: " << avg_confidence << std::endl;
}</std::filesystem::path></optimizedwiredetector></double></std::filesystem::path></iomanip></iostream>